﻿// Written by Dale J Williams.
// Licensed under MPL 1.1 unless otherwise specified. 
// http://www.mozilla.org/MPL/MPL-1.1.html

package engineDJW.display.shapes {
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	
	/**
	 * Slice: A concave semi-circle shape.
	 * @author Dale J Williams
	 */
	public class Slice extends Sprite {
		
		protected const RAD			:Number = Math.PI / 180;
		protected const DEG			:Number = 180 / Math.PI;
		
		private var _radius			:Number;
		private var _innerRadius	:Number;
		private var _angle			:Number;
		private var _color			:uint;
		private var _threshhold		:Number;
		
		public function Slice($color:uint, $radius:Number, $angle:Number) {
			init();
			setup($color, $radius, $angle);
		}
		
		private function init():void {
			radius = 100;
			angle = 30;
			threshhold = 1;
		}
		
		public function setup($color:uint, $radius:Number, $angle:Number):void {
			_color = $color;
			_radius = $radius;
			_angle = $angle;
			draw();
		}
		
		protected function draw():void {
			var seg:Number, ang:Number, mid:Number, segs:Number;
			var ax:Number, ay:Number, ix:Number, iy:Number, bx:Number, by:Number;
			var g:Graphics, i:int;
			
			g = graphics;
			g.clear();
			g.beginFill(_color);
			g.moveTo(0, 0);
			
			segs = Math.ceil(Math.abs(_angle) / _threshhold);
			seg = _angle / segs * RAD;
			
			ang = 0;			
			ax = Math.cos(0) * _radius;
			ay = Math.sin(0) * _radius;	
			ix = Math.cos(0) * _innerRadius;
			iy = Math.sin(0) * _innerRadius;
			
			g.moveTo(ix, iy);
			g.lineTo(ax, ay);
			
			for (i=0; i<segs; i++) {
				ang += seg;
				mid = ang - (seg * 0.5);
				bx = Math.cos(ang) * _radius;
				by = Math.sin(ang) * _radius;
				g.lineTo(bx, by);
			}
			
			bx = Math.cos(ang) * _innerRadius;
			by = Math.sin(ang) * _innerRadius;
			g.lineTo(bx, by);
			
			for (i=0; i<segs; i++) {
				ang -= seg;
				mid = ang + (seg * 0.5);
				bx = Math.cos(ang) * _innerRadius;
				by = Math.sin(ang) * _innerRadius;
				g.lineTo(bx, by);
			}
			
			g.lineTo(ax, ay);
		}
		
		public function get radius():Number { return _radius; }		
		public function set radius(value:Number):void {
			_radius = value;
			draw();
		}
				
		public function get innerRadius():Number { return _innerRadius; }		
		public function set innerRadius(value:Number):void {
			_innerRadius = value;
			draw();
		}
		
		public function get angle():Number { return _angle; }		
		public function set angle(value:Number):void {
			if (Math.abs(value) > 360)
				value = 360;
			_angle = value;
			draw();
		}
		
		public function get color():uint { return _color; }		
		public function set color(value:uint):void {
			_color = value;
			draw();
		}
		
		public function get threshhold():Number { return _threshhold; }		
		public function set threshhold(value:Number):void {
			_threshhold = value;
			draw();
		}
		
	}

}